// pages/profile/profile.js
const db = wx.cloud.database()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    usercode:'',
    password:'19880529',
    buttonStyle:'button-enable' //'button-disable'
  },
  usercodeOnchange:function(e){
    this.setData({
      usercode: e.detail.value
    })
  },

  passwordOnchange:function(e){
    this.setData({
      password: "19880529"//e.detail.value
    })
  },
  //系统登录
  login: function () {
    if (this.data.usercode != '' && this.data.password != '') {
     wx.request({
       url: app.globalData.serverUrl + '/login/?code=' + this.data.usercode,
       success(res){
         //声明
         let userinfo = {
           depart: res.data[0]['depart'],
           username: res.data[0]['user_name'],
           openid: res.data[0]['_openid']
         }
         //数据存缓存
         wx.setStorage({
           key: "userinfo",
           data: userinfo
         }),
           //数据存缓存
           wx.setStorageSync('userinfo', userinfo),
           wx.showModal({
             title: '提示',
             content: '登录成功!',
           })
         setTimeout(() => {
           wx.switchTab({
             url: '/pages/xbmeet/xbmeet',
           })
         }, 1000)
       }
       
     })
    }
    else {
      if (this.data.usercode == '') {
        wx.showModal({
          title: '提示',
          content: '请填写登录账号!',
        })
      }
      if (this.data.password == '') {
        wx.showModal({
          title: '提示',
          content: '请填写登录密码!',
        })
      }

    }
  },
  
  //微信云登录
  wx_login:function(){
    if (this.data.usercode !='' && this.data.password !='')
    {
      db.collection('user').where({
        usercode: this.data.usercode
      }).where({
        password: this.data.password
      }).get().then(res=>{
        //声明
        let userinfo={
          depart: res.data[0]['depart'],
          username: res.data[0]['username'],
          openid: res.data[0]['_openid']
        }
        //数据存缓存
        wx.setStorage({
          key: "userinfo",
          data: userinfo
        }),
        //数据存缓存
        wx.setStorageSync('userinfo', userinfo),
        wx.showModal({
          title: '提示',
          content: '登录成功!',
        })
      }).catch(err=>{
         console.log(err)
      })
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/xbmeet/xbmeet',
        })
      }, 1000)
    }
    else
    {
      if (this.data.usercode==''){
         wx.showModal({
           title: '提示',
           content: '请填写登录账号!',
         })
       }
       if(this.data.password=='')
       {
         wx.showModal({
           title: '提示',
           content: '请填写登录密码!',
         })
       }

    }
  },

  btnclear:function(e){
      this.setData({
        usercode: '',
        password: 'xibei',
      })
      wx.clearStorage();
      wx.clearStorageSync();
  },
  //我的预订
  gethc:function(e){
    wx.redirectTo({
      url: '/pages/mylist/mylist',
    })
  },
  //清理缓存
  clear:function(e){
     var that = this;
     wx.getStorage({
       key: 'userinfo',
       success: function(res) {
         var tem = res.data.usercode
         wx.showModal({
           title: 'xxx',
           content: tem,
         })
       },
       fail:function(res){
         wx.showModal({
           title: 'xxx',
           content: '无缓存记录',
         })
       }
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})